/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 指定登记&发行网站： https://www.zhiqim.com/ 欢迎加盟知启蒙，[编程有你，知启蒙一路随行]。
 *
 * 本文采用《知启蒙许可证》，除非符合许可证，否则不可使用该文件！
 * 1、您可以免费使用、修改、合并、出版发行和分发，再授权软件、软件副本及衍生软件；
 * 2、您用于商业用途时，必须在原作者指定的登记网站，按原作者要求进行登记；
 * 3、您在使用、修改、合并、出版发行和分发时，必须包含版权声明、许可声明，及保留原作者的著作权、商标和专利等知识产权；
 * 4、您在互联网、移动互联网等大众网络下发行和分发再授权软件、软件副本及衍生软件时，必须在原作者指定的发行网站进行发行和分发；
 * 5、您可以在以下链接获取一个完整的许可证副本。
 * 
 * 许可证链接：http://zhiqim.org/licenses/zhiqim_register_publish_license.htm
 * 
 * 除非法律需要或书面同意，软件由原始码方式提供，无任何明示或暗示的保证和条件。详见完整许可证的权限和限制。
 */
package org.zhiqim.lottery.action;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.httpd.validate.onex.IsNumeric;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.kernel.util.Sqls;
import org.zhiqim.lottery.LotteryBootstrap;
import org.zhiqim.lottery.dbo.LotteryInfo;
import org.zhiqim.lottery.dbo.LotteryUser;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 抽奖信息
 *
 * @version v1.4.1 @author zouzhigang 2021-4-17 新建与整理
 */
public class LotteryAction extends StdSwitchAction implements ZmrConstants
{
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNumeric("lotteryId","抽奖编号不能为空"));
    }
    
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("lotteryName", "抽奖名称不能为空"));
        request.addValidate(new IsNumeric("lotteryLine", "抽奖显示行数不能为空"));
        request.addValidate(new IsNumeric("lotteryRow", "抽奖显示列表不能为空"));
        request.addValidate(new IsNumeric("lotteryLuckNum", "抽奖总中奖数不能为空"));
        request.addValidate(new IsNumeric("lotteryLuckPreNum", "抽奖每次中奖数不能为空"));
    }

    protected void list(HttpRequest request) throws Exception
    {
        int page = request.getParameterInt("page");
        int pageSize = request.getContextAttributeInt(ZMR_PAGE_SIZE, 20);
        
        Selector selector = new Selector();
        selector.addMaybeLike("lotteryName", request.getParameter("lotteryName"));
        selector.addMaybe("lotteryStatus", request.getParameterInt("lotteryStatus"));
        selector.addOrderbyDesc("lotteryActive");
        selector.addOrderbyAsc("lotteryCreated");
        
        PageResult<LotteryInfo> result = ORM.table().page(LotteryInfo.class, page, pageSize, selector);
        result.addConditionMap(request.getParameterMap());
        
        request.setAttribute("result", result);
    }

    protected void insert(HttpRequest request) throws Exception
    {
        LotteryInfo lottery = request.getParameter(LotteryInfo.class);
        lottery.setLotteryId(Ids.longId13());
        lottery.setLotteryCreated(Sqls.nowTimestamp());
        
        ORM.table().insert(lottery);
    }

    protected void modify(HttpRequest request) throws Exception
    {
        long lotteryId = request.getParameterLong("lotteryId");
        LotteryInfo item = ORM.table().item(LotteryInfo.class, lotteryId);
        if(item == null)
        {
            request.returnHistory("该抽奖信息不存在，请重新选择");
            return;
        }
        
        request.setAttribute("item", item);
    }
    
    protected void update(HttpRequest request) throws Exception
    {
        LotteryInfo lottery = request.getParameter(LotteryInfo.class);
        
        Updater updater = new Updater();
        updater.addMust("lotteryId", lottery.getLotteryId());
        updater.addField("lotteryName", lottery.getLotteryName());
        updater.addField("lotteryLine", lottery.getLotteryLine());
        updater.addField("lotteryRow", lottery.getLotteryRow());
        updater.addField("lotteryLuckNum", lottery.getLotteryLuckNum());
        updater.addField("lotteryLuckPreNum", lottery.getLotteryLuckPreNum());
        updater.addField("lotteryUserNameType", lottery.getLotteryUserNameType());
        
        ORM.table().update(LotteryInfo.class, updater);
    }

    protected void delete(HttpRequest request) throws Exception
    {
        long lotteryId = request.getParameterLong("lotteryId");
        LotteryInfo item = ORM.table().item(LotteryInfo.class, lotteryId);
        if (item == null)
        {
            request.returnHistory("该抽奖信息不存在");
            return;
        }
        
        ORM.table().delete(LotteryInfo.class, lotteryId);
        
        if (item.isLotteryActive())
        {//清除活动
            request.getContextAttribute(LotteryBootstrap.class).setActiveLotteryId(0);
        }
    }
    
    /** 重置该活动 */
    public void reset(HttpRequest request) throws Exception
    {
        long lotteryId = request.getParameterLong("lotteryId");
        if (lotteryId == -1 || ORM.table().count(LotteryInfo.class, lotteryId) == 0)
        {
            request.setResponseError("该抽奖信息不存在");
            return;
        }
        
        ORM.table().update(LotteryInfo.class, new Updater().addMust("lotteryId", lotteryId).addField("lotteryStatus", 0));
        ORM.table().update(LotteryUser.class, new Updater().addMust("lotteryId", lotteryId).addField("userLuck", 0));
    }
    
    /** 激活该活动 */
    public void active(HttpRequest request) throws Exception
    {
        long lotteryId = request.getParameterLong("lotteryId");
        if (lotteryId == -1 || ORM.table().count(LotteryInfo.class, lotteryId) == 0)
        {
            request.setResponseError("该抽奖信息不存在");
            return;
        }
        
        //设置活动
        request.getContextAttribute(LotteryBootstrap.class).setActiveLotteryId(lotteryId);
        ORM.table().update(LotteryInfo.class, new Updater().addMust("lotteryId", lotteryId).addField("lotteryActive", true));
        ORM.table().update(LotteryInfo.class, new Updater().addMustNotEqual("lotteryId", lotteryId).addField("lotteryActive", false));
    }
    
    /** 关闭该活动 */
    public void close(HttpRequest request) throws Exception
    {
        long lotteryId = request.getParameterLong("lotteryId");
        if (lotteryId == -1 || ORM.table().count(LotteryInfo.class, lotteryId) == 0)
        {
            request.setResponseError("该抽奖信息不存在");
            return;
        }
        
        //设置活动
        request.getContextAttribute(LotteryBootstrap.class).setActiveLotteryId(0);
        ORM.table().update(LotteryInfo.class, new Updater().addMust("lotteryId", lotteryId).addField("lotteryActive", false));
    }
}